package cn.hxzy.teacher.service.impl;

import cn.hxzy.common.domain.Task;
import cn.hxzy.common.domain.TaskManager;
import cn.hxzy.common.domain.User;
import cn.hxzy.common.domain.vo.TaskUser;
import cn.hxzy.common.util.ThreadLocalUtil;
import cn.hxzy.course.mapper.TaskMapper;
import cn.hxzy.teacher.mapper.TaskManagerMapper01;
import cn.hxzy.teacher.service.TaskManagerService01;

import cn.hxzy.user.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaskManagerServicelmpl01 implements TaskManagerService01 {
    @Autowired
    private TaskManagerMapper01 taskManagerMapper01;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public TaskManager getTaskManager(Integer id) {
        TaskManager taskManager = taskManagerMapper01.selectById(id);
        Map map = taskMapper.selectTaskById(taskManager.getTaskId());
        taskManager.setTaskName((String) map.get("task_name"));
        User user = userMapper.selectById02(taskManager.getStudentId());
        taskManager.setStudentName(user.getNickName());
        return taskManager;
    }

    @Override
    public Map selectTaskUser(TaskUser taskUser) {
        List<TaskUser> taskUsers = taskManagerMapper01.selectTaskUser(
                taskUser.getUserId(), taskUser.getTaskId(),taskUser.getClassId());
        Map m = new HashMap();
        m.put("data",taskUsers);
        return m ;
    }

    // 查询章节和章节
    @Override
    public Map seectChapterAndSection(TaskUser taskUser) {

        String chapterName =
                taskManagerMapper01.selectChapterNameById(taskUser.getUserId(), taskUser.getSubmitTime());
        List<Map> sectionName =
                taskManagerMapper01.selectSectionNameById(taskUser.getUserId(), taskUser.getSubmitTime());
        Map m = new HashMap();
        m.put("chapterName",chapterName);
        m.put("sectionName",sectionName);
        return m;
    }

    //批改作业
    @Override
    public int updatetaskManager(Map map) {
        //创建对象 将修改的值赋进去
        TaskManager taskManager = new TaskManager();
        try {
            Integer id = Integer.parseInt((String) map.get("id"));
            Double score = null;
            try {
                String scoreStr = (String) map.get("score");
                if (scoreStr != null) {
                    score = Double.parseDouble(scoreStr);
                }
            } catch (Exception e) {
                // 处理数字格式错误的情况
               e.printStackTrace();
            }
            System.out.println(score+"------------");
            taskManager.setId(id);
            taskManager.setScore(score);
            taskManager.setComment((String) map.get("comment"));
            taskManager.setApprovalStatus(0);
            //后端获取当前登录人信息 把批改教师维护进去
            User user = ThreadLocalUtil.getUser();
            taskManager.setTeacherId(Math.toIntExact(user.getUserId()));
        }catch (Exception e){
            e.printStackTrace();
        }

        return taskManagerMapper01.updateById(taskManager);
    }

    @Override
    public List<Map> selectClassNameById(Integer id) {
        return taskManagerMapper01.selectClassNameById(id);
    }
}
